package com.byyy.ccts.modules.ysjl.api.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializeFilter;
import com.byyy.ccts.core.base.ILogBase;
import com.byyy.ccts.core.common.controller.BaseCRUDController;
import com.byyy.ccts.core.model.AjaxJson;
import com.byyy.ccts.core.model.PageJson;
import com.byyy.ccts.core.query.data.PropertyPreFilterable;
import com.byyy.ccts.core.query.data.Queryable;
import com.byyy.ccts.core.query.utils.QueryableConvertUtils;
import com.byyy.ccts.core.query.wrapper.EntityWrapper;
import com.byyy.ccts.core.utils.StringUtils;
import com.byyy.ccts.modules.ysjl.entity.YsjlActivity;
import com.byyy.ccts.modules.ysjl.entity.YsjlActivityEnroll;
import com.byyy.ccts.modules.ysjl.entity.YsjlLog;
import com.byyy.ccts.modules.ysjl.entity.YsjlUser;
import com.byyy.ccts.modules.ysjl.service.IYsjlActivityEnrollService;
import com.byyy.ccts.modules.ysjl.service.IYsjlActivityService;
import com.byyy.ccts.modules.ysjl.service.IYsjlLogService;
import com.byyy.ccts.modules.ysjl.service.IYsjlUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lyb
 * @date 2022-06-22 13:33
 * @description XX
 */
@Controller
@RequestMapping("/api/ysjl/activity")
public class YsjlActivityController extends BaseCRUDController<YsjlActivity, String> implements ILogBase {

    @Autowired
    private IYsjlActivityService ysjlActivityService;

    @Autowired
    private IYsjlActivityEnrollService ysjlActivityEnrollService;

    @Autowired
    private IYsjlUserService ysjlUserService;

    @Autowired
    private IYsjlLogService ysjlLogService;

    /**
     * 查询活动名单详情
     */
    @RequestMapping(value = "/pageList", method = { RequestMethod.GET, RequestMethod.POST })
    @ResponseBody
    public void pageList(Queryable queryable, PropertyPreFilterable propertyPreFilterable, HttpServletRequest request,
                         HttpServletResponse response) {
        EntityWrapper<YsjlActivity> entityWrapper = new EntityWrapper<YsjlActivity>(YsjlActivity.class);
        // 预处理
        QueryableConvertUtils.convertQueryValueToEntityValue(queryable, YsjlActivity.class);
        SerializeFilter filter = propertyPreFilterable.constructFilter(YsjlActivity.class);
        PageJson<YsjlActivity> pagejson = new PageJson<YsjlActivity>(this.ysjlActivityService.pageActivityList(queryable, entityWrapper));
        String content = JSON.toJSONString(pagejson, filter);
        StringUtils.printJson(response, content);
    }

    /**
     * 查询活动名单详情
     */
    @RequestMapping(value = "/userPageList", method = { RequestMethod.GET, RequestMethod.POST })
    @ResponseBody
    public void userPageList(Queryable queryable, PropertyPreFilterable propertyPreFilterable, HttpServletRequest request,
                         HttpServletResponse response) {
        EntityWrapper<YsjlActivity> entityWrapper = new EntityWrapper<YsjlActivity>(YsjlActivity.class);
        // 预处理
        QueryableConvertUtils.convertQueryValueToEntityValue(queryable, YsjlActivity.class);
        SerializeFilter filter = propertyPreFilterable.constructFilter(YsjlActivity.class);
        PageJson<YsjlActivity> pagejson = new PageJson<YsjlActivity>(this.ysjlActivityService.queryUserActivityListPage(queryable, entityWrapper));
        String content = JSON.toJSONString(pagejson, filter);
        StringUtils.printJson(response, content);
    }

    /**
     * 查询活动日志详情
     */
    @RequestMapping(value = "/logPageList", method = { RequestMethod.GET, RequestMethod.POST })
    @ResponseBody
    public void logPageList(Queryable queryable, PropertyPreFilterable propertyPreFilterable, HttpServletRequest request,
                             HttpServletResponse response) {
        EntityWrapper<YsjlLog> entityWrapper = new EntityWrapper<YsjlLog>(YsjlLog.class);
        // 预处理
        QueryableConvertUtils.convertQueryValueToEntityValue(queryable, YsjlLog.class);
        SerializeFilter filter = propertyPreFilterable.constructFilter(YsjlLog.class);
        PageJson<YsjlLog> pagejson = new PageJson<YsjlLog>(this.ysjlLogService.logPageList(queryable, entityWrapper));
        String content = JSON.toJSONString(pagejson, filter);
        StringUtils.printJson(response, content);
    }

    /**
     * 查询首页活动
     * 1.当前时间点后所有未完成活动列表
     * 2.当前登录用户参与的活动
     */
    @RequestMapping(value = "/home", method = { RequestMethod.GET, RequestMethod.POST })
    @ResponseBody
    public AjaxJson home(@RequestBody YsjlUser ysjlUser) {
        AjaxJson json = new AjaxJson();
        List<YsjlActivity> activityList = this.ysjlActivityService.queryList(ysjlUser.getId());
        List<YsjlActivity> myActivityList = this.ysjlActivityService.queryMyActivityList(ysjlUser.getId());
        Map<String, List<YsjlActivity>> resultMap = new HashMap<>();
        resultMap.put("newList", activityList);
        resultMap.put("myList", myActivityList);
        json.setData(resultMap);
        return json;
    }

    /**
     * 活动详情
     */
    @RequestMapping(value = "/activityInfo", method = { RequestMethod.GET, RequestMethod.POST })
    @ResponseBody
    public AjaxJson activityInfo(@RequestBody YsjlActivity activity) {
        AjaxJson json = new AjaxJson();
        YsjlActivity activityInfo = this.ysjlActivityService.activityInfo(activity.getId(), activity.getUserId());
        json.setData(activityInfo);
        return json;
    }

    /**
     * 后台管理提交活动
     */
    @RequestMapping(value = "/submit", method = { RequestMethod.GET, RequestMethod.POST })
    @ResponseBody
    public AjaxJson submit(HttpServletRequest request, @RequestBody YsjlActivity activity) {
        AjaxJson json = new AjaxJson();
        String ret = this.ysjlActivityService.submitActivity(request, activity);
        if (StringUtils.isNotEmpty(ret)) {
            json.fail(ret);
            return json;
        }
        json.setData(activity);
        return json;
    }

    /**
     * 后台管理提交活动
     */
    @RequestMapping(value = "/update", method = { RequestMethod.GET, RequestMethod.POST })
    @ResponseBody
    public AjaxJson update(@RequestBody YsjlActivity activity) {
        AjaxJson json = new AjaxJson();
        String ret = this.ysjlActivityService.updateActivity(activity);
        if (StringUtils.isNotEmpty(ret)) {
            json.fail(ret);
            return json;
        }
        json.setData(activity);
        return json;
    }

    /**
     * 通过活动名单报名，可多人报
     */
    @RequestMapping(value = "/join", method = { RequestMethod.GET, RequestMethod.POST })
    @ResponseBody
    public AjaxJson join(@RequestBody List<YsjlActivityEnroll> enrollList) {
        AjaxJson json = new AjaxJson();
        String ret = this.ysjlActivityService.join(enrollList);
        if (StringUtils.isNotEmpty(ret)) {
            json.fail(ret);
        }
        return json;
    }

    /**
     * 后台修改活动状态
     */
    @RequestMapping(value = "/status", method = { RequestMethod.GET, RequestMethod.POST })
    @ResponseBody
    public AjaxJson status(@RequestBody YsjlActivity activity) {
        AjaxJson json = new AjaxJson();
        YsjlActivity ysjlActivity = this.ysjlActivityService.selectById(activity.getId());
        ysjlActivity.setStatus(activity.getStatus());
        this.ysjlActivityService.updateById(ysjlActivity);
        return json;
    }

    /**
     * 后台删除活动
     */
    @RequestMapping(value = "/deleteActivity", method = { RequestMethod.GET, RequestMethod.POST })
    @ResponseBody
    public AjaxJson deleteActivity(@RequestBody YsjlActivity activity) {
        AjaxJson json = new AjaxJson();
        this.ysjlActivityService.deleteActivity(activity.getId());
        return json;
    }

    /**
     *
     */
    @RequestMapping(value = "/generateQRCode", method = { RequestMethod.GET, RequestMethod.POST })
    @ResponseBody
    public AjaxJson generateQRCode(HttpServletRequest request, @RequestBody YsjlActivity activity) {
        AjaxJson json = new AjaxJson();
        String qrPic = this.ysjlActivityService.generateQRCode(request, activity.getId(), activity.getQr());
        json.setData(qrPic);
        return json;
    }
    /**
     *  活动报名信息
     * */

    /**
     * 通过活动名单删除报名
     */
    @RequestMapping(value = "/deleteEnroll", method = { RequestMethod.GET, RequestMethod.POST })
    @ResponseBody
    public AjaxJson deleteEnroll(@RequestBody YsjlActivityEnroll activityEnroll) {
        AjaxJson json = new AjaxJson();
        String ret = this.ysjlActivityService.deleteEnroll(activityEnroll);
        if (StringUtils.isNotEmpty(ret)) {
            json.fail(ret);
        }
        // TODO 发送信息
        return json;
    }

    /**
     * 我的活动信息
     */
    @RequestMapping(value = "/myJoinInfo", method = { RequestMethod.GET, RequestMethod.POST })
    @ResponseBody
    public AjaxJson myJoinInfo(@RequestBody YsjlActivity activity) {
        AjaxJson json = new AjaxJson();
        YsjlActivity ysjlActivity = this.ysjlActivityService.myJoinInfo(activity.getId(), activity.getUserId());
        json.setData(ysjlActivity);
        // TODO 发送信息
        return json;
    }

    /**
     * 查询参与活动名单详情
     */
    @RequestMapping(value = "/pageJoinList", method = { RequestMethod.GET, RequestMethod.POST })
    @ResponseBody
    public void pageJoinList(Queryable queryable, PropertyPreFilterable propertyPreFilterable, HttpServletRequest request,
                              HttpServletResponse response) {
        EntityWrapper<YsjlActivityEnroll> entityWrapper = new EntityWrapper<YsjlActivityEnroll>(YsjlActivityEnroll.class);
        // 预处理
        QueryableConvertUtils.convertQueryValueToEntityValue(queryable, YsjlActivityEnroll.class);
        SerializeFilter filter = propertyPreFilterable.constructFilter(YsjlActivityEnroll.class);
        PageJson<YsjlActivityEnroll> pagejson = this.ysjlActivityEnrollService.pageEnrollList(queryable, entityWrapper);
        String content = JSON.toJSONString(pagejson, filter);
        StringUtils.printJson(response, content);
    }

}
